package com.cldz.service;

import com.cldz.common.dto.res.FsuDeviceResDto;
import com.cldz.common.dto.res.FsuResDto;
import com.cldz.common.dto.res.FsuSpResDto;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.common.dto.req.FsuDto;
import com.cldz.common.dto.req.FsuSpDto;
import com.cldz.entity.Fsu;
import com.cldz.entity.FsuSp;
import com.cldz.utils.BeanCopyUtil;
import com.cldz.utils.MathUtils;
import com.cldz.dao.FsuMapper;
import com.cldz.common.dto.req.FsuDeviceDto;
import com.cldz.entity.FsuDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 9:50 2018/12/21
 * @ Description：
 * @ Modified By：
 */
@Service("fsuService")
public class FsuService {

    @Autowired
    private UserPropertyMapper userPropertyMapper;

    @Autowired
    private FsuMapper fsuMapper;

    public List<FsuResDto> getFsuGetDevices(FsuDto fsuDto){
        setAreaID(fsuDto);
        List<Fsu> fsuGetDevices = fsuMapper.getFsuGetDevices(fsuDto);
        return copyListBean(fsuGetDevices);
    }

    public Integer getFsuGetDevicesAmount(FsuDto fsuDto){
        return fsuMapper.getFsuGetDevicesAmount(fsuDto);
    }

    public List<FsuResDto> getFsuGetDevicesWithStation(FsuDto fsuDto){
        List<Fsu> fsuGetDevices = fsuMapper.getFsuGetDevicesWithStation(fsuDto);
        return copyListBean(fsuGetDevices);
    }

    public Integer getFsuGetDevicesWithStationAmount(FsuDto fsuDto){

        return fsuMapper.getFsuGetDevicesWithStationAmount(fsuDto);
    }

    public FsuDto getFsuGetSpecifyDevice(String suID) {
        Fsu fsu = fsuMapper.getFsuGetSpecifyDevice(suID);
        FsuDto dto = new FsuDto();
        BeanCopyUtil.copyPropertiesIgnoreNull(fsu, dto);
        return dto;
    }

    public List<FsuDeviceResDto> getFsuGetSunDev(String suID) {
        List<FsuDevice> list = fsuMapper.getFsuGetSunDev(suID);
        return copyListBeanFsuDev(list);
    }

    public List<FsuSpResDto> getFsuGetSpID(FsuSpDto fsuSpDto) {
        List<FsuSp> list = fsuMapper.getFsuGetSpID(fsuSpDto);
        return copyListBeanFsuSp(list);
    }


    private void setAreaID(FsuDto fsuDto) {
        Integer areaID = fsuDto.getAreaID();
        if (areaID == null) {
            areaID = userPropertyMapper.queryAreaIDByUserID(fsuDto.getUserID());
        }
        fsuDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));
    }



    private List<FsuResDto> copyListBean(List<Fsu> devListByTypeID) {
        List<FsuResDto> list = new ArrayList<>();
        for (Fsu device : devListByTypeID) {
            FsuResDto dto = new FsuResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }

    private List<FsuDeviceResDto> copyListBeanFsuDev(List<FsuDevice> devListByTypeID) {
        List<FsuDeviceResDto> list = new ArrayList<>();
        for (FsuDevice device : devListByTypeID) {
            FsuDeviceResDto dto = new FsuDeviceResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }

    private List<FsuSpResDto> copyListBeanFsuSp(List<FsuSp> devListByTypeID) {
        List<FsuSpResDto> list = new ArrayList<>();
        for (FsuSp device : devListByTypeID) {
            FsuSpResDto dto = new FsuSpResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }



}
